﻿using Dimeng.ArdisPostProcessor.HomagLC4.Ardis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SpreadsheetGear;

namespace Dimeng.ArdisPostProcessor.HomagLC4Adsn
{
    public class DataExporter
    {
        public static void ExportXLSX(R41Data data, string savepath)
        {
            IWorkbook book = Factory.GetWorkbook();
            IWorksheet sheetParts = book.Worksheets.Add();
            sheetParts.Name = "板件";

            IRange cellParts = sheetParts.Cells;

            cellParts[0, 0].Value = "PartRef";
            cellParts[0, 1].Value = "PartQty";
            cellParts[0, 2].Value = "PartW";
            cellParts[0, 3].Value = "PartL";
            cellParts[0, 4].Value = "PartRemark";
            cellParts[0, 5].Value = "PartRemark2";
            cellParts[0, 6].Value = "PartRemark3";
            cellParts[0, 7].Value = "PartD";
            cellParts[0, 8].Value = "PartDrawing";
            cellParts[0, 9].Value = "PartFigDef";
            cellParts[0, 10].Value = "PartFigID";
            cellParts[0, 11].Value = "PartFigParam";
            cellParts[0, 12].Value = "PartFigPartID";
            cellParts[0, 13].Value = "PartMatNo";
            cellParts[0, 14].Value = "PartNo";
            for (int i = 0; i < data.Parts.Count; i++)
            {
                cellParts[i + 1, 0].Value = data.Parts[i].PartRef;
                cellParts[i + 1, 1].Value = data.Parts[i].PartQty;
                cellParts[i + 1, 2].Value = data.Parts[i].PartW;
                cellParts[i + 1, 3].Value = data.Parts[i].PartL;
                cellParts[i + 1, 4].Value = data.Parts[i].PartRemark;
                cellParts[i + 1, 5].Value = data.Parts[i].PartRemark2;
                cellParts[i + 1, 6].Value = data.Parts[i].PartRemark3;
                cellParts[i + 1, 7].Value = data.Parts[i].PartD;
                cellParts[i + 1, 8].Value = data.Parts[i].PartDrawing;
                cellParts[i + 1, 9].Value = data.Parts[i].PartFigDef;
                cellParts[i + 1, 10].Value = data.Parts[i].PartFigID;
                cellParts[i + 1, 11].Value = data.Parts[i].PartFigParam;
                cellParts[i + 1, 12].Value = data.Parts[i].PartFigPartID;
                cellParts[i + 1, 13].Value = data.Parts[i].PartMatNo;
                cellParts[i + 1, 14].Value = data.Parts[i].PartNo;
            }

            IWorksheet sheetPatterns = book.Worksheets.Add();
            sheetPatterns.Name = "锯切";
            IRange cellCuts = sheetPatterns.Cells;

            int counter = 1;
            for (int i = 0; i < data.Patterns.Count; i++)
            {
                var pa = data.Patterns[i];
                cellCuts[counter, 0].Value = i + 1;
                cellCuts[counter, 1].Value = pa.PatternL;
                cellCuts[counter, 2].Value = pa.PatternW;
                cellCuts[counter, 3].Value = $"{pa.PatternTopTrim}/{pa.PatternRightTrim}/{pa.PatternBottomTrim}/{pa.PatternLeftTrim}";
                cellCuts[counter, 4].Value = pa.PatternQty;
                cellCuts[counter, 5].Value = pa.PatternMaterial;
                counter++;

                var cuts = data.FullCuts.Where(it => it.CutLevel == 1 && it.CutPatternNo == pa.PatternNumber).ToList();
                exportCuts(cuts, data.FullCuts, cellCuts, ref counter);
            }

            book.SaveAs(savepath, FileFormat.OpenXMLWorkbook);
        }

        private static void exportCuts(List<ExternCut> cuts, List<ExternCut> fullcuts, IRange cell, ref int counter)
        {
            foreach (var c in cuts)
            {
                if (c.CutType == CutType.SawCut)
                {
                    continue;
                }

                cell[counter, c.CutLevel].Value = c.CutType.ToString();
                cell[counter, c.CutLevel + 1].Value = c.CutW;
                cell[counter, c.CutLevel + 2].Value = c.CutD;
                cell[counter, c.CutLevel + 3].Value = c.CutPartNo;
                counter++;

                if (c.CutType == CutType.Strip)
                {
                    var children = fullcuts.Where(it => it.CutParentNo == c.CutNo).ToList();
                    exportCuts(children, fullcuts, cell, ref counter);
                }
            }
        }
    }
}
